//
//  WWDailyForecastCollectionViewCell.m
//  WhatWeather
//
//  Created by byn on 16/3/19.
//  Copyright © 2016年 byn. All rights reserved.
//

#import "WWDailyForecastCollectionViewCell.h"
#import "WWCommonUtility.h"
#import "WWWeekWeatherCollectionCell.h"
#import "WWCharmDate.h"
#import "PNChart.h"
#import "Masonry.h"
#import "WWWeekWeatherNightCollectionViewCell.h"

@interface WWDailyForecastCollectionViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *tempretureTodayLabel;
@property (weak, nonatomic) IBOutlet UILabel *weekDayOfTodayLabel;
@property (weak, nonatomic) IBOutlet UILabel *weatherInfoLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *dailyForecastWeatherCollection;
@property (nonatomic ,strong) WWWeatherInfo *weatherInfo;
@property (nonatomic ,strong) PNLineChart *lineChart;
@property (weak, nonatomic) IBOutlet UICollectionView *dailyWeekNightCollection;
@end

@implementation WWDailyForecastCollectionViewCell


- (void)awakeFromNib {
    _dailyForecastWeatherCollection.delegate = self;
    _dailyForecastWeatherCollection.dataSource = self;
    [_dailyForecastWeatherCollection registerNib:[UINib nibWithNibName:@"WeekWeather" bundle:nil] forCellWithReuseIdentifier:@"WeekWeather"];
    _dailyWeekNightCollection.delegate = self;
    _dailyWeekNightCollection.dataSource = self;
    [_dailyWeekNightCollection registerNib:[UINib nibWithNibName:@"WeekWeatherNight" bundle:nil] forCellWithReuseIdentifier:@"WeekWeatherNight"];
    
    self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 300, SCREEN_WIDTH, 200)];
    [self.contentView addSubview:self.lineChart];
}

- (void)buildChart {
    [self.lineChart setBackgroundColor:[UIColor clearColor]];
    [self.lineChart setXLabels:@[@"",@"",@"",@"",@"",@"",@""]];
    self.lineChart.yFixedValueMax = 30;
    self.lineChart.yFixedValueMin = 0;
    
    NSMutableArray *dayTemArray = [[NSMutableArray alloc] init];
    for (DailyForecast *forcast in _weatherInfo.dailyForecast) {
        [dayTemArray addObject:forcast.temMax];
    }
    PNLineChartData *dayData = [PNLineChartData new];
    dayData.color = [UIColor whiteColor];
    dayData.itemCount = [dayTemArray count];
    dayData.inflexionPointStyle = PNLineChartPointStyleTriangle;
    dayData.getData = ^(NSUInteger index) {
        CGFloat yValue = [dayTemArray[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    
    NSMutableArray *nightTemArray = [[NSMutableArray alloc] init];
    for (DailyForecast *forcast in _weatherInfo.dailyForecast) {
        [nightTemArray addObject:forcast.temMin];
    }
    PNLineChartData *nightData = [PNLineChartData new];
    nightData.color = [UIColor whiteColor];
    nightData.itemCount = [nightTemArray count];
    nightData.inflexionPointStyle = PNLineChartPointStyleTriangle;
    nightData.getData = ^(NSUInteger index) {
        CGFloat yValue = [nightTemArray[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };
    
    
    self.lineChart.chartData = @[dayData,nightData];
    [self.lineChart strokeChart];
}

- (void)configureCell : (WWWeatherInfo *)weatherInfo {
    
    _weatherInfo = weatherInfo;
    
    _tempretureTodayLabel.text = weatherInfo.nowTmp;
    WWCharmDate *charmDate = [WWCharmDate createDateWithDataStr:weatherInfo.loc withDataFormatter:@"yyyy-MM-dd hh:mm"];
    _weekDayOfTodayLabel.text = [WWCommonUtility getWeekDayOfToday:[charmDate getWeekDay]];
    _weatherInfoLabel.text = weatherInfo.nowWeatherTxt;
    [_dailyForecastWeatherCollection reloadData];
    [self buildChart];
        //[self layoutIfNeeded];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 7;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    return CGSizeMake(width/7, 128);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([collectionView isEqual: _dailyWeekNightCollection]) {
        
        WWWeekWeatherNightCollectionViewCell *weekWeatherNightCollectionViewCell = (WWWeekWeatherNightCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"WeekWeatherNight" forIndexPath:indexPath];
        
        [weekWeatherNightCollectionViewCell configureCell:[_weatherInfo.dailyForecast objectAtIndex:indexPath.row]];
        
        return weekWeatherNightCollectionViewCell;
    }
    else if ([collectionView isEqual: _dailyForecastWeatherCollection]) {
        
        WWWeekWeatherCollectionCell *weekWeatherCollectionView = (WWWeekWeatherCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"WeekWeather" forIndexPath:indexPath];
        
        [weekWeatherCollectionView configureWeatherCollectionCell:[_weatherInfo.dailyForecast objectAtIndex:indexPath.row]  withIndex:indexPath.row];
        
        return weekWeatherCollectionView;
    }
    return nil;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0;
}

- (void)updateConstraints {
        //[self buildConstraints];
    
    [super updateConstraints];
}

- (void)buildConstraints {
    
    [self.lineChart mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_dailyForecastWeatherCollection.mas_bottom).offset(30);
        make.left.equalTo(self.contentView.mas_left);
    }];
}

@end
























